﻿$(function () {

    $("#ReplyToAPost").click(function () {
        var postCreatorEmail = $("#postCreatorEmail").val();
        var name = $("#ReplierName").val();
        var phone = $("#ReplierPhone").val();
        var email = $("#ReplierEmail").val();
        var message = $("#ReplyMessage").val();
        var inValid = false;
        if ($.trim(email) == "") {
            $("#ErrorMsg").text("[ Please provide your email to reply to the post. ]");
            inValid = true;
        }
        else {
            $("#ErrorMsg").text("");
        };
        if ($.trim(message) == "") {
            $("#ErrorMsg").text("[ Please input message to reply to the post. ]");
            inValid = true;
        };
        if (!inValid) {
            $.ajax({
                url: '/NepaliCorner/ReplyToAPost',
                type: 'POST',
                data: {
                    PostCreatorEmail: postCreatorEmail,
                    ReplierName: name,
                    ReplierPhone: phone,
                    ReplierEmail: email,
                    ReplyMessage: message
                },
                content: 'application/json; charset=utf-8',
                success: function (data) {
                    if (data.success == "true") {
                        $("#ReplySection").hide();
                        alert("Thank you for replying to this post. The creator of this post has been notified with your message.");
                    }
                    else {
                        alert("Your email address is not valid. Please input a valid email address.");
                    }
                },
                error: function () {
                    alert("An error has occured while processing your request.");
                }
            });
        }
    });
});